package org.example.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.SubCategoryDto;
import org.example.dto.DeptDto;
import org.example.entity.SysCategory;
import org.example.entity.SysDept;
import org.example.entity.SysSubCategory;
import org.example.mapper.SysSubCategoryMapper;
import org.example.mapper.SysSpuMapper;
import org.example.model.PageBean;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysSubCategoryService {

    @Resource
    private SysSubCategoryMapper sysSubCategoryMapper;

    @Resource
    private SysSpuMapper sysSpuMapper;

    //获取子分类列表
    public PageBean<SysSubCategory> getPageData(Integer page, Integer size, Integer id){
        Page<SysSubCategory> categoryPage = PageHelper.startPage(page, size);
        sysSubCategoryMapper.getListByGridCategoryId(id);
        return PageBean.<SysSubCategory>builder()
                .data(categoryPage.getResult())
                .total(categoryPage.getTotal())
                .current(categoryPage.getPageNum())
                .build();
    }

    //添加子分类
    public void addSubCategory(SubCategoryDto subCategoryDto){
        sysSubCategoryMapper.insert(subCategoryDto);
    }

    //更具id查找子种类信息编辑页面回显
    public SysSubCategory getById(Integer id){
        return sysSubCategoryMapper.selectById(id);
    }

    //编辑种类
    public  void editCategory(SubCategoryDto subCategoryDto){
        subCategoryDto.setCreateTime(new Date());
        sysSubCategoryMapper.updateById(subCategoryDto);
    }

    public void deleteById(Integer id){
        // deleteById() 一定通过 @TableId 来表示谁是id
        sysSubCategoryMapper.deleteById(id);
        sysSpuMapper.deleteBySubCategoryId(id);
    }

    public List<SysSubCategory> getData(){
        return sysSubCategoryMapper.selectList(null);
    }

    public List<SysSubCategory> getAll() {
        return sysSubCategoryMapper.getSubCategoryData();
    }
    //回显操作
    public SysSubCategory getById(Long id) {
        return sysSubCategoryMapper.selectById(id);
    }

    //编辑数据
    public void edit(SysSubCategory sysSubCategory) {
        System.out.println("service--------传过来的id"+sysSubCategory.getId());
        sysSubCategoryMapper.updateById(sysSubCategory);
    }



}
